package cj.web.tb.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.login.controllers.BaseController;
import cj.web.tb.dao.TBAreaDAO;
import cj.web.tb.domain.TBArea;

@Path("/tBArea")
public class TBAreaController extends BaseController{

	private static Logger logger = Logger.getLogger(TBAreaController.class.getName());
	
	@Autowired
	private TBAreaDAO tBAreaDao;
	
	@Get("show")
	public String show(Model model,TBArea tBArea) {
		return "/views/tb/areaList.vm";
	}

	@Post("getList")
	public String queryList(TBArea tBArea) {
		//当前页  
		tBArea.setStart((tBArea.getPage()-1)*tBArea.getRows());
		tBArea.setLength(tBArea.getRows());
		
		JSONObject jo = new JSONObject();
		
		List<TBArea> objs = tBAreaDao.query(tBArea);
		
		int size = tBAreaDao.querySize(tBArea);
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(){
		return "/views/tb/areaDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,TBArea tBArea,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			tBArea.setCreateUser(returnSessionObj(request).getAccount());
			tBArea.setUpdateUser(returnSessionObj(request).getAccount());
			tBAreaDao.create(tBArea);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		TBArea obj = tBAreaDao.queryById(id);
		
		model.add("obj", obj);
		return "/views/tb/areaEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,TBArea tBArea,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			tBArea.setUpdateUser(returnSessionObj(request).getAccount());
			tBAreaDao.update(tBArea);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(","))
		{
			tBAreaDao.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}